
/*
 * 文件名：RelationMapServiceImpl.java.
 * 描述：
 * 创建人: 龚洪亮
 * 创建时间：2017年8月9日
 */

package com.fiberhome.platform.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fiberhome.platform.common.EUDataGridResult;
import com.fiberhome.platform.common.ExcelUtil;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.entity.MapOutputInfo;
import com.fiberhome.platform.entity.RelationMapDetail;
import com.fiberhome.platform.mapper.DesignNeedMapper;
import com.fiberhome.platform.mapper.RelationMapMapper;
import com.fiberhome.platform.mapper.TestCaseMapper;
import com.fiberhome.platform.mapper.TestCategoryMapper;
import com.fiberhome.platform.pojo.DesignNeedExample;
import com.fiberhome.platform.pojo.RelationMap;
import com.fiberhome.platform.pojo.RelationMapExample;
import com.fiberhome.platform.pojo.RelationMapExample.Criteria;
import com.fiberhome.platform.pojo.TestCaseExample;
import com.fiberhome.platform.pojo.TestCategory;
import com.fiberhome.platform.pojo.TestCategoryExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * @Description 
 * @author 龚洪亮
 * @version 2017年8月9日
 */
@Service
public class RelationMapServiceImpl implements RelationMapService {
	
	@Autowired
	private RelationMapMapper relationMapMapper;
	
	@Autowired
	private DesignNeedMapper designNeedMapper;
	
	@Autowired
	private TestCaseMapper testCaseMapper;
	
	@Autowired
	private TestCategoryMapper testCatMapper;
	
	
	@Override
	public EUDataGridResult getMapList(Integer page, Integer rows) {
		RelationMapExample example = new RelationMapExample();
		PageHelper.startPage(page, rows);
		List<RelationMap> list = relationMapMapper.selectByExample(example);
		
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		PageInfo<RelationMap> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public EUDataGridResult getMapListOneHour(Integer page, Integer rows) {
		RelationMapExample example = new RelationMapExample();
		Criteria criteria = example.createCriteria();
		Date time = null;
		time = new Date();
		Calendar   calendar = new GregorianCalendar(); 
	    calendar.setTime(time);
	    calendar.add(Calendar.HOUR, -1);
		criteria.andCreatedTimeGreaterThan(calendar.getTime());
		
		
		PageHelper.startPage(page, rows);
		List<RelationMap> list = relationMapMapper.selectByExample(example);
		
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		PageInfo<RelationMap> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	@Override
	@Transactional
	public PageResult delteMap(String ids) {
		try {
			String[] id_list = ids.split(",");
			for (String id : id_list) {
				relationMapMapper.deleteByPrimaryKey(Integer.parseInt(id));
			}
			return PageResult.ok();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return PageResult.build(500, "删除失败！");
	}

	@Override
	@Transactional
	public PageResult createMap(RelationMap map) {
		map.setCreatedTime(new Date());
		if (relationMapMapper.insert(map) > 0) {
			return PageResult.ok(map);
		} else {
			return PageResult.build(300, "创建失败！");
		}
	}

	@Override
	public PageResult isBuild(RelationMap map) {
		RelationMapExample example = new RelationMapExample();
		Criteria criteria = example.createCriteria();
		criteria.andDesignNoEqualTo(map.getDesignNo());
		criteria.andCaseNoEqualTo(map.getCaseNo());
		List<RelationMap> list = relationMapMapper.selectByExample(example);
		if (list.size()==0) {
			return PageResult.ok();
		}
		return PageResult.ok(list.get(0));
	}

	
	@Override
	public RelationMapDetail getMapDetail(Integer id) {
		RelationMap map = relationMapMapper.selectByPrimaryKey(id);
		String designNo = map.getDesignNo();
		String caseNo = map.getCaseNo();

		DesignNeedExample example1 = new DesignNeedExample();
		TestCaseExample example2 = new TestCaseExample();
		
		RelationMapDetail detail = new RelationMapDetail();
		
		DesignNeedExample.Criteria criteria1 = example1.createCriteria();
		criteria1.andDesignNoEqualTo(designNo);
		detail.setDesignNeed(designNeedMapper.selectByExample(example1).get(0));
		
		TestCaseExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andCaseNoEqualTo(caseNo);
		detail.setTestCase(testCaseMapper.selectByExample(example2).get(0));
		
		return detail;
	}

	@Override
	public Workbook writeExcel(String ids) {
		String idArray[] = ids.split(",");
		List<RelationMap> maps = new ArrayList<RelationMap>();
		List<MapOutputInfo> outputs = new ArrayList<MapOutputInfo>();
		
		for (String i:idArray) {
			maps.add(relationMapMapper.selectByPrimaryKey(Integer.parseInt(i)));
		}
		//获取了建立好的映射后，将设计需求编号用逗号分隔
		
		//testSet是用于保存测试用例条数的
		Set<String> testSet = new HashSet<>();
		//mapList是用于保存
		List<RelationMap> mapList = new ArrayList<RelationMap>();
		for(RelationMap m:maps) {
			if (!testSet.contains(m.getCaseNo())) {
				mapList.add(m);
			}
			testSet.add(m.getCaseNo());
			
		}
		int id=1;
		for(RelationMap m:mapList) {
			//根据relationmap的caseNo获取testCategory的id，然后在获取parentList
			String caseNo = m.getCaseNo();
			TestCategoryExample test_example = new TestCategoryExample();
			test_example.createCriteria().andCaseNoEqualTo(caseNo);
			Integer test_id = testCatMapper.selectByExample(test_example).get(0).getId();
			List<TestCategory> catList = testCatMapper.selectAllParent(test_id);
			
			RelationMapExample example = new RelationMapExample();
			example.createCriteria().andCaseNoEqualTo(caseNo);
			List<RelationMap> needs = relationMapMapper.selectByExample(example);
			String design_needs = "";
			String design_needs_name="";
			for(RelationMap n:needs) {
				design_needs+=n.getDesignNo()+",";
				design_needs_name+=n.getDesignText()+"|";
			}
			
			design_needs = design_needs.substring(0, design_needs.length()-1);
			design_needs_name = design_needs_name.substring(0, design_needs_name.length()-1);
			MapOutputInfo o = new MapOutputInfo();
			o.setNeedNo(design_needs);
			o.setNeedName(design_needs_name);
			o.setTestNo(caseNo);
			o.setItem(m.getCaseText());
			o.setId(id);
			o.setCatList(catList);
			outputs.add(o);
			id++;
		}
    	try {
			ExcelUtil util = new ExcelUtil();
			File file = util.getExcelDemoFile("demo.xls");
			Workbook wb = null;
			wb = util.writeNewExcel(file, "sheet1", outputs);
			return wb;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
         
		return null;
	}

	@Override
	public EUDataGridResult queryMapByCat(Integer id, Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		List<RelationMap> list = relationMapMapper.queryMapByCat(id);
		
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		PageInfo<RelationMap> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

}
